import { _decorator, AudioClip, AudioSourceComponent, Component, Director, director, Node } from 'cc';
const { ccclass } = _decorator;

@ccclass('SoundManager')
export class SoundManager extends Component {
    private static instance: SoundManager | null = null;
    private audioSourceNode: Node | null = null;
    private bgmAudioSource: AudioSourceComponent | null = null;
    private sfxAudioSource: AudioSourceComponent | null = null;
    private bgmIsMuted: boolean = false;
    private sfxIsMuted: boolean = false;

    private bgmVolume: number = 1;

    public static getInstance(): SoundManager {
        if (!this.instance) {
            this.instance = new SoundManager();
            // 监听场景加载事件
            director.on(Director.EVENT_AFTER_SCENE_LAUNCH, () => {
                this.instance?.createAudioSourceNode();
            });
            // 监听场景切换事件
            director.on(Director.EVENT_BEFORE_SCENE_LOADING, () => {
                this.instance?.cleanup();
            });
        }
        return this.instance;
    }

    private constructor() {
        super();
        // 初始化音频节点
        this.createAudioSourceNode();
    }

    private createAudioSourceNode() {
        // 清理旧节点
        if (this.audioSourceNode?.isValid) {
            this.audioSourceNode.destroy();
        }

        // 创建新节点
        this.audioSourceNode = new Node("AudioSourceNode");
        const scene = director.getScene();
        if (scene) {
            scene.addChild(this.audioSourceNode);
        }

        // 添加音频组件
        this.bgmAudioSource = this.audioSourceNode.addComponent(AudioSourceComponent);
        this.sfxAudioSource = this.audioSourceNode.addComponent(AudioSourceComponent);
        this.bgmAudioSource.loop = true;

        // 设置为常驻节点
        this.audioSourceNode.setParent(null!);
        director.addPersistRootNode(this.audioSourceNode);
    }

    public cleanup() {
        // 清理节点和组件
        if (this.audioSourceNode?.isValid) {
            this.audioSourceNode.destroy();
        }
        this.audioSourceNode = null;
        this.bgmAudioSource = null;
        this.sfxAudioSource = null;
    }

    // 播放背景音乐
    public playBGM(clip: AudioClip, volume: number) {
        try {
            if (!this.bgmAudioSource?.isValid) {
                this.createAudioSourceNode();
            }
            if (this.bgmAudioSource) {
                this.bgmAudioSource.clip = clip;
                this.bgmVolume = volume;
                this.bgmAudioSource.volume = this.bgmIsMuted ? 0 : this.bgmVolume;
                this.bgmAudioSource.play();
            }
        } catch (e) {
            console.error('BGM play failed:', e);
        }
    }

    // 停止播放背景音乐
    public stopBGM() {
        if (this.bgmAudioSource?.isValid) {
            this.bgmAudioSource.stop();
        }
    }

    // 播放音效
    public playSFX(clip: AudioClip) {
        try {
            if (!this.sfxAudioSource?.isValid) {
                this.createAudioSourceNode();
            }
            if (this.sfxAudioSource) {
                this.sfxAudioSource.clip = clip;
                this.sfxAudioSource.play();
            }
        } catch (e) {
            console.error('SFX play failed:', e);
        }
    }

    // 切换BGM静音状态
    public toggleBGMMute() {
        this.bgmIsMuted = !this.bgmIsMuted;
        if (this.bgmAudioSource?.isValid) {
            this.bgmAudioSource.volume = this.bgmIsMuted ? 0 : this.bgmVolume;
        }
    }

    // 切换SFX静音状态
    public toggleSFXMute() {
        this.sfxIsMuted = !this.sfxIsMuted;
        if (this.sfxAudioSource?.isValid) {
            this.sfxAudioSource.volume = this.sfxIsMuted ? 0 : 1;
        }
    }

    // 获取BGM静音状态
    public getBGMMuteStatus(): boolean {
        return this.bgmIsMuted;
    }

    // 获取SFX静音状态
    public getSFXMuteStatus(): boolean {
        return this.sfxIsMuted;
    }
}